package com.ge.med.cchelper.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

public class PropHelper {
	
	public final static String CONFIG_FILE="cchelper.properties";
	public final static String HOST="host";
	public final static String USERNAME="userid";
	public final static String VIEWNAME="view";
	public final static String VOBDIR="vobs";
	public final static String CLEARTOOL="cleartool";
	public final static String VOB_DOWNLOAD="vobsdownload";
	public final static String SYNC_ALL_REGEX="sync.all.regex";
	
	public static String readValue(String filePath, String key) {
		Properties props = new Properties();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(
					filePath));
			props.load(in);
			String value = props.getProperty(key);
			//System.out.println(key + value);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Properties read(String filePath) {
		Properties props = new Properties();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(
					filePath));
			props.load(in);
			return props;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void readProperties(String filePath) {
		Properties props = new Properties();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(
					filePath));
			props.load(in);
			Enumeration en = props.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String Property = props.getProperty(key);
				System.out.println(key + Property);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void writeProperties(String filePath, String parameterName,
			String parameterValue) {
		Properties prop = new Properties();
		try {
			InputStream fis = new FileInputStream(filePath);

			prop.load(fis);

			OutputStream fos = new FileOutputStream(filePath);
			prop.setProperty(parameterName, parameterValue);
			prop.store(fos, "Update '" + parameterName + "' value");
		} catch (IOException e) {
			LogUtil.err("Visit " + filePath + " for updating "
					+ parameterName + " value error");
		}
	}
	
	public static void writeProperties(String filePath, HashMap map) {
		Properties prop = new Properties();
		try {
			InputStream fis = new FileInputStream(filePath);

			prop.load(fis);

			OutputStream fos = new FileOutputStream(filePath);
			Iterator it=map.keySet().iterator();
			String tempKeys = "";
			while(it.hasNext()){
			   String key=(String)it.next();
			   tempKeys = tempKeys+" "+key;
			   prop.setProperty(key, (String)map.get(key));
			}
			
			prop.store(fos, "Update map '" + tempKeys + "' value");
		} catch (IOException e) {
			LogUtil.err("Visit " + filePath + " for updating map value error");
		}
	}

	public static void main(String[] args) {}
}
